﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data.Common;

using MINERP.DAL.UserLogin;
using MINERP.DAL;
using MINERP.MODEL.FoundationModel;


namespace MINERP.BLL.UserLogin
{
    public class UserLoginBLL
    {
        private List<tb_Power> _powerList = null;
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pwd"></param>
        public void UserLogin(string userName, string pwd, out List<string> controlName, out Dictionary<int, string> techName,out  users userObj)
        {
            userObj = null;
            controlName = new List<string>();
            techName = new Dictionary<int, string>();
            userObj = UserLoginDAL.GetUsers(userName, pwd);
            ObjectQuery<string> controls = UserLoginDAL.GetControlName(userName, pwd);
            foreach (string control in controls)
            {
                controlName.Add(control);
            }
           
            //_powerList = UserLoginDAL.GetPowerList();
            List<int> techIDList = new List<int>();
            //foreach (tb_Power power in _powerList)
            //{
            //    foreach (tb_Power powerOfUser in userObj.tb_Power)
            //    {
            //        if (power.Name == powerOfUser.Name)
            //        {
            //            foreach (tb_Tech tech in power.tb_Tech)
            //            {

            //                if (!techIDList.Contains(tech.TechId))
            //                {
            //                    techIDList.Add(tech.TechId);
            //                    //techName.Add(tech.TechId, tech.TechName);
            //                    techName.Add(tech.TechId, tech.FrmName);
            //                }
            //            }
            //        }
            //    }
            //}

            techName = UserLoginDAL.GetTechFormNam(userObj.userID);
        }
        /// <summary>
        /// 判断该账号能否通过验证
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public bool IsUserPass(string userName, string pwd)
        {
            bool flag = false ;
            users user = UserLoginDAL.GetUsers(userName, pwd);
            if (user !=null)
            {
                if (user.IsUseful)
                {
                    flag = true;
                }
                else
                {
                    flag = false;
                    throw new Exception("该用户已停用");
                }                
            }
            else
            {
                flag = false;
                throw new Exception("账号密码错误");
            }

            return flag;
        }

        /// <summary>
        /// 判断该账号是否是组长
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>



        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List< tb_Power> GetPowerList()
        {
            return UserLoginDAL.GetPowerList();
        }

        /// <summary>
        /// 得到用户的权限列表
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public List<PowerEntity> GetPowerByUserID(int userid)
        {
            List<tb_Power> list = UserLoginDAL.GetPowerByUserID(userid);
            List<PowerEntity> collection = new List<PowerEntity>();

            foreach (tb_Power item in list)
            {
                collection.Add(new PowerEntity(item));
            }

            return collection;
        }
        /// <summary>
        ///  修改用户密码
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="oldPwd"></param>
        /// <param name="newPwd"></param>
        /// <returns></returns>
        public int ChangePwd(string userName, string oldPwd, string newPwd)
        {
            int ret =0 ;
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                users user = entities.users.FirstOrDefault(u => u.LoginName.ToLower() == userName.ToLower() && u.LoginPwd.ToLower() == oldPwd.ToLower());
                if (user == null)
                {
                    ret = -1;
                }
                else
                {
                    user.LoginPwd = newPwd.ToLower();
                    entities.SaveChanges();
                }
            }
            return ret;
        }
        public int ChangeUserIsOnLine(users user, bool flag)
        {
            int ret = 0;
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                users obj = entities.users.FirstOrDefault(u => u.LoginName == user.LoginName && u.LoginPwd == user.LoginPwd);
                if (obj == null)
                {
                    ret = -1;
                }
                else
                {
                    obj.IsOnline = flag;
                    entities.SaveChanges();
                }
            }
            return ret;
        }
        public void Save()
        {
            DALBase.Save();
        }
        public Dictionary<int,string> GetMuliRole(int inUserId)
        {
            Dictionary<int,string> techlist=new Dictionary<int, string>();
            using (newSampleYPDBEntities db=new newSampleYPDBEntities())
            {
                users obj = db.users.FirstOrDefault(u =>u.userID==inUserId);
                if (obj == null)
                {
                   return null;
                }
                else
                {
                    obj.tb_Tech.Load();
                    List<tb_Tech> techs = obj.tb_Tech.ToList();
                    foreach (tb_Tech list in techs)
                    {
                        techlist.Add(list.TechId, list.TechName);
                    }
                    return techlist;
                }
            }
        }

        /// <summary>
        /// 获得车间信息
        /// </summary>
        /// <returns></returns>
        public WorkShop_Info GetWorkShopInfo()
        {
            WorkShop_Info wsInfo =null;
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    WorkShop ws = db.WorkShop.FirstOrDefault();
                    if (ws != null)
                    {
                        wsInfo = new WorkShop_Info()
                        {
                            Address = ws.Address,
                            AutoAssignShop = ws.AutoAssignShop,
                            CustomerTel = ws.CustomerTel,
                            Email = ws.Email,
                            FinanceTel = ws.FinanceTel,
                            QQ = ws.QQ,
                            Remark = ws.Remark,
                            WorkShopName = ws.WorkShopName,
                            InsertEPByPhotoSpec = ws.InsertEmployeePerfermanceByPhotoSpec

                        };
                    }
                }
            }
            catch
            {
              
            }
            return wsInfo;
        }

    }

    #region //权限实体

    public class PowerEntity
    {
        private tb_Power _obj = null;

        #region //constructor

        public PowerEntity()
        { 
        
        }

        public PowerEntity(tb_Power obj)
        {
            this._obj = obj;
        }

        #endregion

        #region //public priorities

        public int PowerId
        {
            get
            {
                return this._obj.PowerId;
            }
        }

        public string Name
        {
            get
            {
                return this._obj.Name;
            }
        }

        public string Desc
        {
            get
            {
                return this._obj.Des;
            }
        }

        public string FrmName
        {
            get
            {
                return this._obj.FrmName;
            }
        }

        public string AssemblyName
        {
            get
            {
                return this._obj.AssemblyName;
            }
        }
        #endregion
    }

    #endregion

}
